package com.wu.store.service.impl;

import com.wu.store.dao.CartDao;
import com.wu.store.dao.OrderDao;
import com.wu.store.dao.UserDao;
import com.wu.store.entity.*;
import com.wu.store.service.CartService;
import com.wu.store.service.OrderService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {
	@Autowired
	private CartDao cartDao;
	@Autowired
	private OrderDao orderDao;

	@Autowired
	private UserDao userDao;

	@Autowired
	private CartService cartService;

	@Override
	public List<Order> list(Long uid) throws Exception {
		List<Long> statusNot = new ArrayList<>();
		statusNot.add(-1L);
		return orderDao.list(uid, statusNot);
	}

	@Override
	public OrderInfo info(Long uid, Long orderId) throws Exception {
		Order order = orderDao.selectById(orderId);
		if (order == null) {
			throw new Exception("找不到该订单");
		}
		if (!order.getUid().equals(uid)) {
			throw new Exception("无权访问");
		}
		if (order.getStatus() == -1) {
			throw new Exception("该订单已删除");
		}
		OrderInfo orderInfo = new OrderInfo();
		BeanUtils.copyProperties(order, orderInfo);
		List<InstGoods> instList = orderDao.selectInstByOrderId(orderId);
		orderInfo.setInstList(instList);
		orderInfo.setUser(userDao.getUserByUid(order.getUid()));
		return orderInfo;
	}

	@Override
	@Transactional
	public Long add(Long uid, List<Long> idList) throws Exception {
		Order order = new Order();
		order.setUid(uid);
		order.setCreateTime(new Date());
		List<OrderInst> instList = new ArrayList<>();
		List<Cart> cartList = cartDao.selectByUidAndIdIn(uid, idList);
		if (cartList.size() == 0) {
			throw new Exception("订单无商品");
		}
		Cart firstCart = cartList.get(0);
		String name = firstCart.getGoodsName();
		if (cartList.size() > 1) {
			name += " 等" + cartList.size() + "个商品";
		}
		order.setName(name);
		order.setStatus(0);
		float money = 0f;
		List<Long> cartIdList = new ArrayList<>();
		for (Cart cart : cartList) {
			cartIdList.add(cart.getId());
			OrderInst orderInst = new OrderInst();
			orderInst.setGoodsId(cart.getGoodsId());
			orderInst.setPrice(cart.getGoodsPrice());
			orderInst.setCount(cart.getGoodsCount());
			instList.add(orderInst);
			money += cart.getGoodsPrice() * cart.getGoodsCount();
		}
		order.setMoney(money);
		int affectedRow = orderDao.add(order);
		Long orderId = order.getId();
		if (affectedRow == 0) {
			throw new Exception("创建订单失败");
		}
		for (OrderInst orderInst : instList) {
			orderInst.setOrderId(orderId);
		}
		orderDao.batchAddInst(instList);
		cartService.batchDelete(uid, cartIdList);
		return orderId;
	}


	@Override
	public boolean update(Order order) throws Exception{
		int up = orderDao.update(order);
		if(up != 0) {
			return true;
		}else {
			throw new Exception("更新失败");
		}
	}
}
